const webpack = require('webpack')
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: './src/main.js',
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, 'dist'),
    },
    devtool: 'eval-cheap-module-source-map',
    devServer: {
        hotOnly: true,
    },
    module: {
        rules: [
            {
                test: /.css$/, //正则表达式
                use: ['style-loader', 'css-loader'], //使用的Loader
            },
            {
                test: /.(jpg|png|jpeg|svg)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 10 * 1024,  //限制文件最大大小，超出使用file-loader，否则使用url-loader
                        esModule: false,
                    },
                },
            },
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        attrs: ['img:src', 'a:href'],
                    }
                }
            },
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: 'webpack plugin sample', //文件的title
        }),
        // 需要用到webpack的内置HMR插件
        new webpack.HotModuleReplacementPlugin()
    ],
}